<?php

require_once './application/core/CoreBase.php';
require_once './application/core/Client/detailSoap.php';
require_once './application/models/Tables/cache.php';
require_once './application/models/Objets/Composant.php';
require_once 'XML/xmlToObjArray.php';

class detailCore extends CoreBase
{

    private $_data;
    private $_nbrInt;
    private $_ip;
    private $_mask;
    private $_mac;
    private $_status;
    private $_speed;
    private $_descInt;
    private $_id;
    private $_objC;

    public function __construct(&$controller)
    {
        parent::__construct($controller);
        $this->_objC = new ObjectComposant();
    }

    //fait appel a la sonde pour le service detail
    public function detailIp($list, $name)
    {
        $obj = new detailSoap($this->_controller);
        $params = array('ListeIp' => $list, 'Name' => $name);
        $obj->detail($params, 'detailipresp');
        return;
    }

    //execute le parsing et met en base
    public function detailIpResp($xml)
    {
        $obj = new xmlToObjArray();
        $this->_data = $obj->parse_xml_data($xml, false);
        $this->_data = $this->_data[0]['PARAM'][0]['DETAIL'][0]['DATA0'][0]['INFO'][0];
        $this->parseDetail();
    }

    private function putDetailBase($i)
    {
        sleep(2);
        $check = $this->_objC->rechercheDetailComposant($this->_id[0]['IdComposant']);
        if (isset($check[$i - 1]))
        {
            if (array_key_exists('IdDetail', $check[$i - 1]) == true)
                $this->_objC->modifierDetail($check[$i - 1]['IdDetail'], $this->_speed, $this->_mac, $this->_ip, $this->_mask, $this->_status, $this->_descInt);
        }
        else
            $this->_objC->ajouterDetail($this->_id[0]['IdComposant'], $this->_speed, $this->_mac, $this->_ip, $this->_mask, $this->_status, $this->_descInt);
        return;
    }

    private function parseDetail()
    {
        $this->_nbrInt = $this->_data['NBRINTER'][0][0];
        $this->_id = $this->_objC->rechercheComposant($this->_data['HOST'][0][0]);
        for ($i = 1; $i <= $this->_nbrInt; $i++)
        {
            if (array_key_exists('ADDRIP.' . $i, $this->_data) == true)
                $this->_ip = $i . "=" . $this->_data['ADDRIP.' . $i][0][0] . ";";
            else
                $this->_ip = $i . "=;";
            if (array_key_exists('ADDRMASK.' . $i, $this->_data) == true)
                $this->_mask = $i . "=" . $this->_data['ADDRMASK.' . $i][0][0] . ";";
            else
                $this->_mask = $i . "=;";
            if (array_key_exists('MACINTER.' . $i, $this->_data) == true)
                $this->_mac = $i . "=" . $this->_data['MACINTER.' . $i][0][0] . ";";
            else
                $this->_mac = $i . "=;";
            if (array_key_exists('STATUSINTER.' . $i, $this->_data) == true)
                $this->_status = $i . "=" . $this->_data['STATUSINTER.' . $i][0][0] . ";";
            else
                $this->_status = $i . "=;";
            if (array_key_exists('SPEEDINTER.' . $i, $this->_data) == true)
                $this->_speed = $i . "=" . $this->_data['SPEEDINTER.' . $i][0][0] . ";";
            else
                $this->_speed = $i . "=;";
            if (array_key_exists('DESCINTER.' . $i, $this->_data) == true)
                $this->_descInt = $i . "=" . $this->_data['DESCINTER.' . $i][0][0] . ";";
            else
                $this->_descInt = $i . "=;";
            $this->putDetailBase($i);
        }
    }

}